package com.summer.core.schedule.job;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.alibaba.fastjson.JSONObject;
import com.summer.core.util.LocalConstants;
import com.summer.dingding.model.DingDingMessageTemplate;
import com.summer.dingding.model.DingDingUser;
import com.summer.dingding.service.DingDingMessageService;
import com.summer.dingding.service.DingDingService;
import com.summer.order.model.DayFood;
import com.summer.order.model.Food;
import com.summer.order.model.UserDayFood;
import com.summer.order.model.UserOrder;
import com.summer.order.service.UserOrderService;
import com.summer.user.model.User;


public class AutoRemindLunchJob implements Job{

	@Resource
	UserOrderService userOrderService;
	
	@Resource
	DingDingService dingDingService;
	
	@Resource
	DingDingMessageService dingDingMessageService;
	
	@Resource
	HibernateTemplate ht;
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		
		List<UserDayFood> list = userOrderService.getTodayLunchList();
		if (list != null && list.size() > 0) {
			UserDayFood udf = list.get(0);
			List<DayFood> dayFoodList = ht.find("select dayFood from DayFood dayFood where dayFood.date=?",udf.getDate());
			List<Map<String, String>> form = new ArrayList<Map<String,String>>();
			if(dayFoodList != null && dayFoodList.size() > 0) {
				DayFood dayFood = dayFoodList.get(0);
				Food food = null;
				Map<String, String> form1 = null;
				if(dayFood.getFood1() != null) {
					food = ht.get(Food.class, dayFood.getFood1().getId());
					form1 = new HashMap<String, String>();
					form1.put("key", "午餐一：");
					form1.put("value", food.getName());
					form.add(form1);
				}
				if(dayFood.getFood2() != null) {
					food = ht.get(Food.class, dayFood.getFood2().getId());
					String key = "午餐二：";
					if(form1.get("key") == null || form1.get("key").equals("")) {
						key = "午餐一：";
					}
					form1 = new HashMap<String, String>();
					form1.put("key", key);
					form1.put("value", food.getName());
					form.add(form1);
				}
			}
			int pre = new Double(Math.random() * 993).intValue() + 1;
			
			DingDingMessageTemplate message = dingDingMessageService.buildSendMessage("http://www.summor.cn", "吃饭了！！！", "按时吃饭，多喝热水~~~", "老夏", LocalConstants.Config.LOCAL_URL + "/include/images/emoticon/" + pre + ".jpg", form);
			int size = list.size();
			for (int i = 0; i <=  size / 20; i++) {
				StringBuffer sb = new StringBuffer();
				int x = (i+1) * 20 > size ? size :(i+1) * 20;
				List<UserDayFood> newList = list.subList(i*20, x);
//				int sizeNew = newList.size();
				
				for (UserDayFood userDayFood : newList) {
					UserOrder userOrder = ht.get(UserOrder.class, userDayFood.getUserOrder().getId());
					User user = ht.get(User.class, userOrder.getUser().getId());
					if(user.getDuser() != null) {
						
						DingDingUser duser = ht.get(DingDingUser.class, user.getDuser().getId());
						if(duser.getBindStatus() != null || duser.getBindStatus().equals("1")) {
							sb.append(duser.getId()).append(",");
						}
					}
				}
				if(sb.toString().length() > 0 ) {
					System.out.println(sb);
					dingDingService.sendDDMessage(sb.substring(0,sb.length() - 1).trim(), message);
				}
			}
			
		}
		
	}
}
